import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/login/api/login_api.dart';
import 'package:honeypotflutter/net/public_httpInfo.dart';
import 'package:honeypotflutter/public/public_alert.dart';
import 'package:honeypotflutter/public/public_get.dart';
import 'package:honeypotflutter/public/public_storage.dart';
import 'package:honeypotflutter/publicBussine/bussine_constants.dart';
import 'package:honeypotflutter/publicBussine/loginToken/LoginTokenModel.dart';
import 'package:honeypotflutter/public/public_total.dart';
import 'package:honeypotflutter/router/bussine_router_url.dart';

class LoginLogic extends GetxController {
  final TextEditingController phoneController = TextEditingController(text: "18135771809");
  final TextEditingController pwdController = TextEditingController(text: "Aa1234567890");
  final TextEditingController codeController = TextEditingController();

  /// 是否是密码登录
  RxBool isPwdLogin = RxBool(true);
  RxBool isEnable = RxBool(false);

  String codeToken = "";

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    isEnable.value = pwdController.text.isNotEmpty && phoneController.text.isNotEmpty;
    phoneController.addListener(() {
      _changeButtonAction();
    });
    pwdController.addListener(() {
      _changeButtonAction();
    });
    codeController.addListener(() {
      _changeButtonAction();
    });
  }

  /*
  * 修改
  * */
  _changeButtonAction() {
    if (isPwdLogin.value) {
      isEnable.value = phoneController.text.isNotEmpty && pwdController.text.isNotEmpty;
    } else {
      isEnable.value = phoneController.text.isNotEmpty && codeController.text.isNotEmpty;
    }
  }

  /*
  * 判断手机号
  * */
  bool isPhoneNumber() {
    if (!GetUtils.isPhoneNumber(phoneController.text)) {
      HLTotal.total("请输入正确手机号码");
      return false;
    }
    return true;
  }

  /*
  * tabbar选择
   */
  void tabBarSelect(int index) {
    isPwdLogin.value = (index == 0);
    if (index == 0) {
      codeController.text = "";
    } else {
      pwdController.text = "";
    }
  }

  /*
  * 短信
  * */
  sendMessage() async {
    String phone = phoneController.text;
    LoginApi.sendMessage(phone, 18).then((value) {
      codeToken = value;
    });
  }

  /*
  * 登录
  * */
  void loginClick() async {
    String phone = phoneController.text;
    if (!GetUtils.isPhoneNumber(phone)) {
      HLTotal.total("请输入正确手机号码");
      return;
    }

    /// 密码登录
    if (isPwdLogin.value) {
      String pwd = pwdController.text;
      if (pwd.isEmpty) {
        HLTotal.total("请输入密码");
        return;
      }
      LoginApi.loginWithPassword(phone, pwd).then((value) {
        _loginTokenConfiguration(value);
      });
      return;
    }

    /// 短信登录
    if (codeToken.isEmpty) {
      HLTotal.total("请获取验证码");
      return;
    }
    String code = codeController.text;
    if (code.isEmpty) {
      HLTotal.total("请输入验证码");
      return;
    }
    LoginApi.loginWithCode(phone, code, codeToken).then((value) {
      _loginTokenConfiguration(value);
    });
  }

  /*
  * token处理
  * */
  _loginTokenConfiguration(dynamic info) {
    HLStorage.setStorage(MGConstants.login_token_info, info);
    LoginTokenModel model = LoginTokenModel.fromJson(info);
    MGHttpInfo.token = model.accessToken ?? "";
    HLGet.offAllNamed(MGRouterUrl.index);
  }

  /*
  * 弹框
  * */
  showAlert() async {
    HLAlert.showSureCancelAlert(
        title: "退出",
        message: "您确定要退出蜜罐管家",
        cancelLabel: "取消",
        okLabel: "退出",
        sureAction: (bool isOk) {
          if (isOk) {
            exit(0);
          }
        });
  }
}
